unit DruckenConfig;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls, ExtDlgs, jpeg, sqlite3, definition;

type
  TDruckeinstellungenForm = class(TForm)
    Img: TImage;
    Label1: TLabel;
    url: TEdit;
    Button1: TButton;
    Label2: TLabel;
    footer: TEdit;
    Uebernehmen: TButton;
    Abbrechen: TButton;
    OpenPicture: TOpenPictureDialog;
    DelPic: TButton;
    procedure AbbrechenClick(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure UebernehmenClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure DelPicClick(Sender: TObject);
  end;

implementation

{$R *.dfm}

procedure TDruckeinstellungenForm.AbbrechenClick(Sender: TObject);
begin
  Close;
end;

procedure TDruckeinstellungenForm.Button1Click(Sender: TObject);
begin
  if OpenPicture.Execute then
  begin
    url.Text := OpenPicture.FileName;
    Img.Picture.LoadFromFile(OpenPicture.FileName);
  end;
end;

procedure TDruckeinstellungenForm.UebernehmenClick(Sender: TObject);
var
  db: TSqliteDatabase;
begin

  // Write configuration to database

  db := TSqliteDatabase.Open(DBFile);

  db.ExecSQL('update configuration set '
    + 'print_logo="' + StringReplace(url.Text, '"', '', [rfReplaceAll]) + '", '
    + 'print_footer="' + StringReplace(Footer.Text, '"', '', [rfReplaceAll]) + '"' );
  
  db.Free;
  Close;

end;

procedure TDruckeinstellungenForm.FormShow(Sender: TObject);
var
  db: TSqliteDatabase;
begin

  // Get configuration from database

  db := TSqliteDatabase.Open(DBFile);
  
  with db.Query('SELECT print_logo, print_footer FROM configuration limit 1') do
  begin
    if not Eof then
    begin
      if FileExists(FieldAsString(0)) then
      begin
        url.Text := FieldAsString(0);
        Img.Picture.LoadFromFile(FieldAsString(0));
      end else begin
        url.Text := '';
        Img.Picture := nil;
      end;
      Footer.Text := FieldAsString(1);
    end;
    Free;
  end;
  
  db.Free;

end;

procedure TDruckeinstellungenForm.DelPicClick(Sender: TObject);
begin
  url.Text := '';
  Img.Picture := nil;
end;

end.
